package pl.javarsovia.javarsovia4android.adapters;

import static pl.javarsovia.javarsovia4android.util.UIUtils.*;

import java.util.ArrayList;
import java.util.List;

import pl.javarsovia.R;
import pl.javarsovia.javarsovia4android.activities.SessionsActivity;
import pl.javarsovia.javarsovia4android.domain.Conference;
import pl.javarsovia.javarsovia4android.domain.Lecture;
import pl.javarsovia.javarsovia4android.domain.Room;
import pl.javarsovia.javarsovia4android.domain.Slot;
import pl.javarsovia.javarsovia4android.util.StringUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LectureAdapter extends ArrayAdapter<Lecture> {

	private int roomNo = 0;
	private List<Room> rooms;
	private final SessionsActivity activity;

	public LectureAdapter(SessionsActivity activity, int textViewResourceId, Conference conference) {
		super(activity, textViewResourceId, new ArrayList<Lecture>());
		this.activity = activity;
		rooms = conference.getRooms();
		setRoom(0);
	}

	private void setRoom(int roomIdx) {
		refreshRoomName();
		addAll(getRoom().getLectures());
	}

	private void refreshRoomName() {
		fillTextView(activity, R.id.room, getRoom().getName());
	}

	private Room getRoom() {
		return rooms.get(roomNo);
	}

	@Override
	public Lecture getItem(int position) {
		return getLecture(position);
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		view = getView(view);
		Lecture lecture = getLecture(position);
		if (lecture == null)
			return view;
		initRowView(view, lecture);
		view.setClickable(lecture.getSpeaker() == null);
		view.setMinimumHeight(90);
		return view;
	}

	private Lecture getLecture(int position) {
		return getRoom().getLecture(position);
	}

	private void initRowView(View view, Lecture lecture) {
		setSpeakerName(view, lecture);
		fillTextView(view, R.id.lecture_name, lecture.getTitle());
		setTIme(view, lecture.getSlot());
	}

	private void setTIme(View view, Slot slot) {
		fillTextView(view, R.id.start, slot.getStart());
		TextView stopView = (TextView) view.findViewById(R.id.stop);
		if (StringUtils.isEmpty(slot.getFinish()))
			stopView.setVisibility(View.GONE);
		else
			stopView.setText(slot.getFinish());
	}

	private void setSpeakerName(View view, Lecture lecture) {
		TextView speakerNameView = (TextView) view.findViewById(R.id.speaker_name);
		if (lecture.getSpeaker() == null) {
			speakerNameView.setVisibility(View.GONE);
		} else {
			speakerNameView.setVisibility(View.VISIBLE);
			speakerNameView.setText(lecture.getSpeaker().getName());
		}
	}

	private View getView(View view) {
		if (view == null) {
			LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = li.inflate(R.layout.sessions_row, null);
		}
		return view;
	}

	private void addAll(List<Lecture> lectures) {
		clear();
		for (Lecture lecture : lectures)
			add(lecture);
	}

	public void nextSession() {
		if (isNotLastRoom()) {
			roomNo++;
			refreshView();
		}
	}

	private void refreshView() {
		refreshRoomName();
		notifyDataSetChanged();
	}

	public void prevSession() {
		if (isNotFirstRoom()) {
			roomNo--;
			refreshView();
		}
	}

	private boolean isNotFirstRoom() {
		return roomNo - 1 >= 0;
	}

	private boolean isNotLastRoom() {
		return roomNo + 1 < rooms.size();
	}

}
